package com.spzx.channel.service.imp;

import com.spzx.channel.domain.ItemVo;
import com.spzx.channel.service.ItemService;
import com.alibaba.fastjson.JSON;


import com.spzx.common.core.constant.SecurityConstants;


import com.spzx.common.core.domain.R;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.product.api.RemoteProductService;

import com.spzx.product.api.domain.vo.*;
import jakarta.annotation.Resource;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

@Service
public class ItemServiceImpl implements ItemService {
    @Resource
    RemoteProductService remoteProductService;
    @Resource
    RedissonClient redissonClient;
    @Resource
    ThreadPoolExecutor threadPoolExecutor;


    @Override
    public ItemVo getItem(long skuId) {
        ItemVo itemVo = new ItemVo();
//        //远程调用商品微服务接口之前 提前知道用户访问商品SKUID是否存在与布隆过滤器
//        RBloomFilter<Object> bloomFilter = redissonClient.getBloomFilter("sku:bloom:filter");
//        if (!bloomFilter.contains(skuId)) {
//
//            //查询数据不存在直接返回空对象
//            throw new ServiceException("用户查询商品sku不存在");
//        }
        CompletableFuture<ProductSku> completableFutureSku = CompletableFuture.supplyAsync(new Supplier<ProductSku>() {
            @Override
            public ProductSku get() {
                ProductSku productSku = remoteProductService.getProductSku(skuId, SecurityConstants.INNER).getData();
                itemVo.setProductSku(productSku);// sku信息
                return productSku;
            }
        },threadPoolExecutor);
        CompletableFuture<Product> completableFutureProduct = completableFutureSku.thenApplyAsync(new Function<ProductSku, Product>() {
            @Override
            public Product apply(ProductSku productSku) {
                Long productId = productSku.getProductId();
                Product product = remoteProductService.getProduct(productId, SecurityConstants.INNER).getData();
                itemVo.setProduct(product);
                String specValue = product.getSpecValue();
                itemVo.setSpecValueList(JSON.parseArray(specValue));// 规格
                return product;
            }
    }, threadPoolExecutor);
        CompletableFuture completableFutureDetails = completableFutureProduct.thenAcceptAsync(new Consumer<Product>() {
            @Override
            public void accept(Product product) {
                Long productId = product.getId();
                ProductDetails productDetails = remoteProductService.getProductDetails(productId, SecurityConstants.INNER).getData();
                String sliderUrls = product.getSliderUrls();
                itemVo.setSliderUrlList(Arrays.asList(sliderUrls.split(",")));// 轮播图
                String imageUrls = productDetails.getImageUrls();
                itemVo.setDetailsImageUrlList(Arrays.asList(imageUrls.split(",")));// 海报
            }
        }, threadPoolExecutor);
        CompletableFuture<Void> completableFutureMap = completableFutureProduct.thenAcceptAsync(new Consumer<Product>() {
            @Override
            public void accept(Product product) {
                Long productId = product.getId();
                R<Map<String, Long>> stringLongMapR = remoteProductService.getSkuSpecValue(productId, SecurityConstants.INNER);
                Map<String, Long> mapRData = stringLongMapR.getData();
                itemVo.setSkuSpecValueMap(mapRData);// 规格：skuId的map
            }
        }, threadPoolExecutor);

        CompletableFuture<Void> completableFuturePrice = CompletableFuture.runAsync(new Runnable() {
            @Override
            public void run() {
                SkuPrice skuPrice = remoteProductService.getSkuPrice(skuId, SecurityConstants.INNER).getData();
                itemVo.setSkuPrice(skuPrice);// sku价格
            }
        }, threadPoolExecutor);
        CompletableFuture<Void> completableFutureStock = CompletableFuture.runAsync(new Runnable() {
            @Override
            public void run() {
                SkuStockVo skuStockVo = remoteProductService.getSkuStock(skuId, SecurityConstants.INNER).getData();
                itemVo.setSkuStockVo(skuStockVo);// sku库存
            }
        },threadPoolExecutor);
        CompletableFuture.allOf(
                completableFutureSku,
                completableFutureProduct,
                completableFutureDetails,
                completableFutureMap,
                completableFuturePrice,
                completableFutureStock).join();

        // TODO
        return itemVo;
    }


    public ItemVo itemNoMulti(long skuId) {
        //远程调用商品微服务接口之前 提前知道用户访问商品SKUID是否存在与布隆过滤器
        RBloomFilter<Object> bloomFilter = redissonClient.getBloomFilter("sku:bloom:filter");
        if (!bloomFilter.contains(skuId)) {

            //查询数据不存在直接返回空对象
            throw new ServiceException("用户查询商品sku不存在");
        }
        ProductSku productSku = remoteProductService.getProductSku(skuId, SecurityConstants.INNER).getData();
        Long productId = productSku.getProductId();

        Product product = remoteProductService.getProduct(productId, SecurityConstants.INNER).getData();

        ProductDetails productDetails = remoteProductService.getProductDetails(productId, SecurityConstants.INNER).getData();

        SkuPrice skuPrice = remoteProductService.getSkuPrice(skuId, SecurityConstants.INNER).getData();

        SkuStockVo skuStockVo = remoteProductService.getSkuStock(skuId, SecurityConstants.INNER).getData();

        Map<String, Long> stringLongMap = remoteProductService.getSkuSpecValue(productId, SecurityConstants.INNER).getData();

        ItemVo itemVo = new ItemVo();
        itemVo.setSkuSpecValueMap(stringLongMap);// 规格：skuId的map
//        itemVo.setProduct(product);// 商品信息
        itemVo.setProduct(product);
        itemVo.setSkuPrice(skuPrice);// sku价格
        itemVo.setSkuStockVo(skuStockVo);// sku库存
        itemVo.setProductSku(productSku);// sku信息
        String specValue = product.getSpecValue();
        itemVo.setSpecValueList(JSON.parseArray(specValue));// 规格
        String sliderUrls = product.getSliderUrls();
        itemVo.setSliderUrlList(Arrays.asList(sliderUrls.split(",")));// 轮播图
        String imageUrls = productDetails.getImageUrls();
        itemVo.setDetailsImageUrlList(Arrays.asList(imageUrls.split(",")));// 海报
        // TODO
        return itemVo;
    }
}
